<?php
class CompCmsComponent extends Object {
	var $components = array( 'Auth','Session','RequestHandler','Email','Cookie','CompCms');
	
	var $cms_files = array('Cm.id','Cm.controller','Cm.action','Cm.name','Cm.keywords','Cm.description');
	var $cms_section_files = array('CmsSection.id','CmsSection.cms_id','CmsSection.ctp_id','CmsSection.body','CmsSection.status');
	var $page = null;
	var $sections = null;
	
	public function initialize(&$controller) {
        $this->controller = $controller;
        $this->data = $controller->data;  	
    }
    
    /**
         * Page title and content for sections
         */
    public function get(){
    	if(is_array($this->_page())){			
			if(isset($this->page['Cm']['name'])){
				$this->controller->set('title_for_layout_cms',$this->page['Cm']['name']);
				$this->controller->set('keywords_for_layout_cms',$this->page['Cm']['keywords']);
				$this->controller->set('description_for_layout_cms',$this->page['Cm']['description']);												
			}else{
				$this->controller->set('title_for_layout_cms','');
				$this->controller->set('keywords_for_layout_cms','');
				$this->controller->set('description_for_layout_cms','');				
			}		
		}else{
			$this->controller->set('title_for_layout_cms','');
			$this->controller->set('keywords_for_layout_cms','');
			$this->controller->set('description_for_layout_cms','');
		} 
		
		$this->Session->write('sections',$this->_sections());
		$this->Session->write('page',$this->page);
    }
    
    /**
     * 
     * Pobranie danych o stronie
     * @param array $params
     */
    public function get_page($params){
    	$page = $this->controller->Cm->find('first',
					array(
						'conditions'=>
							array('Cm.controller = ' => $params['controller'],
								'Cm.action = ' =>  $params['action']
						)
						,'fields'=>$this->cms_files
					));
    	$this->controller->Cm->create();
		$this->controller->Cm->save($new);
		return $page;
    }
    

    
    /**
     * 
     * Get current page title and keywords
     */
    private function _page(){
    	switch($this->controller->params['controller']){
    		case 'page':
//    			if(!isset($this->controller->params['pass'][0])) break;
    			$this->page = $this->controller->Cm->find('first',
					array(
						'conditions'=>
							array('Cm.controller = ' => $this->controller->params['controller'],
								'Cm.action = ' => $this->controller->params['pass'][0]
						)
						,'fields'=>$this->cms_files
					));
				break;
				
    		default:
		    	$this->page = $this->controller->Cm->find('first',
					array(
						'conditions'=>
							array('Cm.controller = ' => $this->controller->params['controller'],
								'Cm.action = ' => $this->controller->params['action']
						)
						,'fields'=>$this->cms_files
					));    			
    	}

			
		return (is_array($this->page) && !empty($this->page)) ? $this->page : false;
    }
    
    
    
    /**
     * 
     * Get current sections content
     */
    private function _sections(){
		$this->sections = $this->controller->CmsSection->find('all',
			array(
				'conditions'=>
					array('CmsSection.cms_id = '=>$this->page['Cm']['id'])
				,'fields'=>$this->cms_section_files
			));
		$secByCtpID = array();
		if(is_array($this->sections) && !empty($this->sections)){
			foreach ($this->sections as $v) {
				$secByCtpID[$v['CmsSection']['ctp_id']] = $v;
			}
		}
		$this->sections = $secByCtpID;
		return (is_array($this->sections) && !empty($this->sections)) ? $this->sections : false;
    } 
    
    public function _section($params){    	
    	$cm =& $this->getModel('Cm');
	    if(!$cm){
	      return false;
	    }
        $CmsSection =& $this->getModel('CmsSection');
	    if(!$CmsSection){
	      return false;
	    }
	    $cm_id = $cm->field('id',			
					array('Cm.controller = '=>$params['controller'],'Cm.action = '=>$params['action'])
			);

		$section = $CmsSection->find('first',
			array(
				'conditions'=>
					array('CmsSection.cms_id'=>$cm_id,'CmsSection.ctp_id'=>$params['section'])
				,'fields'=>$this->cms_section_files
			));
		return $section;
    }
	
    function &getModel($name = null) {
		$model = null;
		if (!$name) {
			$name = 'Cm';
		}    
		if($name){
			if (PHP5) {
				$model = ClassRegistry::init($name);
			} else {
				$model =& ClassRegistry::init($name);
			}

			if (empty($model)) {
//				$this->_error('FileUpload::getModel() - Model is not set or could not be found');
				return null;
			}
		}
		return $model;
	}   
}